package cn.fourhundredandeighteen.view.Component;

import cn.fourhundredandeighteen.UserInfo;
import org.jdesktop.swingx.VerticalLayout;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.lang.reflect.Constructor;
import java.util.ArrayList;

public class Container extends JFrame {
    private static Container instance = null;

    private ArrayList<LabelButton> labelButtonList = new ArrayList<LabelButton>(); // 页面左边的按钮组
    private JPanel left;    // 页面左边部分
    private JPanel right;   // 页面右边部分
    private JPanel content = new JPanel();  //页面右边部分所显示的内容
    private LabelButton currentButton = null; // 表示当前被点击的按钮，再点击后才有值

    public Container() {

    }

    // 初始化函数
    private Container(ArrayList<LabelButton> labelButtonList) {
        // 初始化按钮组
        setLabelButtonList(labelButtonList);
        //初始化容器
        initComponents();
        this.setTitle("学生信息管理系统");
        this.setLocation(450, 200);

        // 得到显示器屏幕的宽高
        int width = Toolkit.getDefaultToolkit().getScreenSize().width;
        int height = Toolkit.getDefaultToolkit().getScreenSize().height;
        // 定义窗体的宽高
        int windowsWedth = 1280;
        int windowsHeight = 720;
        // 设置窗体居中
        this.setBounds((width - windowsWedth) / 2, (height - windowsHeight) / 2, 0, 0);
        this.setMinimumSize(new Dimension(windowsWedth, windowsHeight));

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
    }

    // 单例模式
    public static Container getInstance(ArrayList<LabelButton> labelButtonList) {
        if (instance == null) {
            instance = new Container(labelButtonList);
        }
        return instance;
    }

    // 单例模式
    public static Container getInstance() {
        return instance;
    }


    // 关闭容器
    public static void close() {
        getInstance().dispose();
        instance = new Container();
        instance = null;
    }


    // 更新页面内容
    public void upDatePage() {
        // 移除页面中的内容
        right.remove(content);

        // 创建按钮所绑定的类
        getContent getContent = new getContent("创建页面内容的线程");
        getContent.start();

        // 等待线程执行完毕
        try {
            getContent.join();
        } catch (InterruptedException interruptedException) {
            interruptedException.printStackTrace();
        }

        // 更新页面
        content.setBackground(Color.white);
        right.add(content, BorderLayout.CENTER);
        right.updateUI();
    }

    // 页面初始化
    private void initComponents() {
        // 设置开屏界面
        Image image = new ImageIcon("src/main/resources/Images/Welcome.png").getImage();
        content = new BackgroundPanel(image);

        // 创建Panel容器
        left = new JPanel();
        right = new JPanel();

        //设置容器的尺寸和颜色
        left.setPreferredSize(new Dimension(120, 0));
        left.setBackground(new Color(32, 34, 42));
        left.setLayout(new VerticalLayout());
        // 设置容器的布局
        right.setBackground(new Color(242, 242, 242));
        right.setLayout(new BorderLayout());

        // 创建空标签,实现留白效果
        JLabel north = new JLabel();
        JLabel west = new JLabel();
        JLabel east = new JLabel();
        JLabel south = new JLabel();
        north.setPreferredSize(new Dimension(0, 20));
        west.setPreferredSize(new Dimension(20, 0));
        east.setPreferredSize(new Dimension(20, 0));
        south.setPreferredSize(new Dimension(0, 20));
        right.add(north, BorderLayout.NORTH);
        right.add(west, BorderLayout.WEST);
        right.add(east, BorderLayout.EAST);
        right.add(south, BorderLayout.SOUTH);

        // 添加按钮组
        for (LabelButton labelButton : labelButtonList) {
            // 为组件添加事件监听器
            labelButton.addMouseListener(new Listener());
            // 添加组件
            left.add(labelButton);
        }

        // 添加内容
        right.add(content);
        this.add(left, BorderLayout.WEST);
        this.add(right, BorderLayout.CENTER);
    }

    public ArrayList<LabelButton> getLabelButtonList() {
        return labelButtonList;
    }

    public void setLabelButtonList(ArrayList<LabelButton> labelButtonList) {
        this.labelButtonList = labelButtonList;
    }

    public JPanel getLeft() {
        return left;
    }

    public void setLeft(JPanel left) {
        this.left = left;
    }

    public JPanel getRight() {
        return right;
    }

    public void setRight(JPanel right) {
        this.right = right;
    }

    public JPanel getContent() {
        return content;
    }

    public void setContent(JPanel content) {
        this.content = content;
    }

    public LabelButton getCurrentButton() {
        return currentButton;
    }

    public void setCurrentButton(LabelButton currentButton) {
        this.currentButton = currentButton;
    }

    // 多线程类
    class getContent extends Thread {  // 继承Thread类，作为线程的实现类
        private String name;       // 表示线程的名称

        public getContent(String name) {
            this.name = name;      // 通过构造方法配置name属性
        }

        @Override
        public void run() { // 覆写run()方法，作为线程 的操作主体
            // 通过反射创建对象
            try {
                // 拿到当前被点击按钮绑定的类
                Class clz = currentButton.content;

                // 拿到类的构造方法
                Constructor appleConstructor;
                try {
                    // 获取有参构造
                    appleConstructor = clz.getConstructor(Long.class);
                    // 将创建的类保存到页面内容中
                    content = (JPanel) appleConstructor.newInstance(UserInfo.Session);
                } catch (NoSuchMethodException noSuchMethodException) {
                    // 拿不到指定构造器就获取无参构造
                    appleConstructor = clz.getConstructor();
                    // 将创建的类保存到页面内容中
                    content = (JPanel) appleConstructor.newInstance();
                } catch (Exception e) {
                    // 显示错误界面
                    content = new ErrorView();
                }
            } catch (Exception classNotFoundException) {
                classNotFoundException.printStackTrace();
            }
        }
    }

    // 利用该内部类来监听所有事件源产生的事件
    class Listener implements MouseListener {

        @Override
        public void mousePressed(MouseEvent e) {  // 鼠标按下事件
            // 移除页面中的内容
            right.remove(content);

            // 拿到当前被点击到按钮
            currentButton = (LabelButton) e.getSource();

            // 创建按钮所绑定的类
            getContent getContent = new getContent("创建页面内容的线程");
            getContent.start();

            // 遍历按钮组 设置样式
            currentButton.setForeground(new Color(32, 34, 42));
            currentButton.setBackground(new Color(242, 242, 242));
            for (LabelButton button : labelButtonList) {
                if (!currentButton.getText().equals(button.getText())) {
                    button.setBackground(new Color(32, 34, 42));
                    button.setForeground(new Color(242, 242, 242));
                }
            }

            // 等待线程执行完毕
            try {
                getContent.join();
            } catch (InterruptedException interruptedException) {
                interruptedException.printStackTrace();
            }

            // 更新页面
            content.setBackground(Color.white);
            right.add(content, BorderLayout.CENTER);
            right.updateUI();
        }

        @Override
        public void mouseClicked(MouseEvent e) {
            // 鼠标点击
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            // 鼠标释放
        }

        @Override
        public void mouseEntered(MouseEvent e) {
            // 鼠标移入
            LabelButton labelButton = (LabelButton) e.getSource();
            labelButton.setBorder(BorderFactory.createMatteBorder(0, 4, 0, 0, new Color(0, 150, 137)));
        }

        @Override
        public void mouseExited(MouseEvent e) {
            // 鼠标移出
            LabelButton labelButton = (LabelButton) e.getSource();
            labelButton.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, new Color(48, 52, 64)));
        }
    }
}